package com.kerry.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.manager.util.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.kerry.aop.MethodLog;
import com.kerry.exception.GolbalExceptionHandler.Result;
import com.kerry.model.User;
import com.kerry.mapper.UserMapper;
import com.kerry.service.UserService;

@RestController
public class UserController{
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/getUsers")
	public List<User> getUsers() {
		
		return userService.getAll();
	}
	
    @RequestMapping("/getUser")
    public User getUser(Integer id) {
    	
        return userService.getOne(id);
    }
    
    @RequestMapping("/add")
    public void save(User user) throws Exception {
    	userService.insert123(user);
    }
    
    @RequestMapping(value="update")
    public void update(User user) {
    	userService.update(user);
    }
    
    @RequestMapping(value="/delete/{id}")
    public void delete(@PathVariable("id") Integer id) {
    	userService.delete(id);
    }
    
    
    @RequestMapping(value="/login")
    public void login(HttpServletRequest req,String username,String password){
//    	userMapper.delete(id);
    	//若存在会话则返回该会话，否则新建一个会话
    	req.getSession(true).setAttribute("username", username);
//    	if (username.equals("")) {//异常
//			
//		}
//    	throw new RuntimeException("hahhah");  
    }
    
    
    /**
     * 只针对当前类生效
     * @return
     */
//	@ExceptionHandler(RuntimeException.class)
//  @ExceptionHandler(value={RuntimeException.class,MyRuntimeException.class})
//    @ExceptionHandler //处理所有异常 
//    @ResponseBody
//    public String exceptionHandler(){
//    	return "this is error msg";
//    }

    
}